package com.eln.medplayer;

import java.io.File;
import java.io.IOException;

import com.eln.medplayer.R;

import com.eln.medplayer.VidActivity;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.Toast;

public class MedPlayer extends Activity {

	ImageButton btnLt;
	ImageButton btnEng;
	ImageButton btnGest;
	private static View progressView;
	private static View mainView;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		try {
			String command;
	        command = "LD_LIBRARY_PATH=/vendor/lib:/system/lib service call activity 42 s16 com.android.systemui";
	        Process proc = Runtime.getRuntime().exec(new String[] { "su", "-c", command });
	        proc.waitFor();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			//e.printStackTrace();
		} catch (InterruptedException e) {}
		super.onCreate(savedInstanceState);
		
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
		
		RelativeLayout relative = new RelativeLayout(this);
		
		mainView=View.inflate(getBaseContext(),R.layout.main_menu,null);
	    relative.addView(mainView);
		
		//progressView = View.inflate(getBaseContext(),R.layout.progressbar, null);
	    //relative.addView(progressView);
		
	    setContentView(relative);
		//setContentView(R.layout.main_menu);
		getView();
		//View v = findViewById(R.id.menuView);
		//
		
		//v.setSystemUiVisibility(View.STATUS_BAR_HIDDEN);
		try {
			String command;
	        command = "LD_LIBRARY_PATH=/vendor/lib:/system/lib service call activity 42 s16 com.android.systemui";
	        Process proc = Runtime.getRuntime().exec(new String[] { "su", "-c", command });
	        proc.waitFor();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			//e.printStackTrace();
		} catch (InterruptedException e) {}
	}
	
	
	public void getView(){
		btnLt = (ImageButton) findViewById(R.id.btnLT);
		btnEng = (ImageButton) findViewById(R.id.btnEN);
		btnGest = (ImageButton) findViewById(R.id.btnGest);
		btnLt.setOnClickListener(LTmenu);
		btnEng.setOnClickListener(Engmenu);
		btnGest.setOnClickListener(Gestmenu);
	}
	
	View.OnClickListener LTmenu = new View.OnClickListener() {
	    public void onClick(View v) {
	      // it was the 1st button
	    	gotoSubMenuScreen(0);
	    };
	  };
	  View.OnClickListener Engmenu = new View.OnClickListener() {
	    public void onClick(View v) {
	      // it was the 2nd button
	    	gotoSubMenuScreen(1);
	    };
	  };
	  View.OnClickListener Gestmenu = new View.OnClickListener() {
		    public void onClick(View v) {
		      // it was the 3nd button
		    	gotoSubMenuScreen(2);
		    };
		  };

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		//getMenuInflater().inflate(R.menu.med_player, menu);
		return true;
	}

	private void gotoSubMenuScreen(int type) {
		if (checkFiles(type)){
        Intent lIntent = new Intent();
        switch (type){
        case 0:lIntent= new Intent(this, VidList.class);lIntent.putExtra("menuOption", 0);break;
        case 1:lIntent= new Intent(this, VidList.class);lIntent.putExtra("menuOption", 1);break;
        case 2:lIntent= new Intent(this, VidList.class);lIntent.putExtra("menuOption", 2);break;
        }
        startActivity(lIntent);
       // MedPlayer.this.overridePendingTransition(
	//			R.anim.slide_in_right,
		//		R.anim.slide_out_left
	    	//	);
        finish();
		}else{
			Toast.makeText(getBaseContext(), "Nerasta Media!", Toast.LENGTH_LONG).show();
		}
    }
	
	private static boolean checkFiles(int option){
	      boolean res=false;
	      File fa1=new File(new String("/storage/sdcard0/audio lt"));
	      File fa2=new File(new String("/storage/sdcard0/audio en"));
	      File fv=new File(new String("/storage/sdcard0/video"));
	      switch (option){
	      case 0: if (fa1.exists()) for(String fn : fa1.list()){ res=true;}break;
	      case 1: if (fa2.exists()) for(String fn : fa2.list()){ res=true;};break;
	      case 2: if (fv.exists()) for(String fn : fv.list()){ res=true;};break;
	      }
		  return res;
		}
	
	@Override
	public void onBackPressed() {

		android.os.Process.killProcess(android.os.Process.myPid());
	    //android.os.Process.killProcess(android.os.Process.myPid());
	    //System.exit(0);
	    // This above line close correctly
	}

}
